<template>
	<view>
		<view class="title">充值金额</view>
		<view class="content">
			<view class="dole">￥</view>
			<input type="digit" class="money" @blur="onInput"  v-model="money" />
		</view>
		<view class="buttonBox">
			<view class="recharge" @click="doPay">充值</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				money:'',
			}
		},
		methods: {
			formatTwoDecimals(input) {
			  // 类型过滤：处理非字符串/数字类型
			  if (typeof input !== 'string' && typeof input !== 'number') {
			    return NaN;
			  }
			
			  // 统一转为字符串处理
			  let str = String(input).trim();
			
			  // 过滤非法字符（保留数字和小数点）
			  str = str.replace(/[^\d.]/g, '');
			
			  // 处理多个小数点（只保留第一个）
			  const dotIndex = str.indexOf('.');
			  if (dotIndex !== -1) {
			    str = str.slice(0, dotIndex + 1) + str.slice(dotIndex + 1).replace(/\./g, '');
			  }
			
			  // 分割整数和小数部分
			  let [integer = '0', decimal = ''] = str.split('.');
			
			  // 处理前导零：00123 → 123
			  integer = integer.replace(/^0+/, '') || '0';
			
			  // 截取最多两位小数
			  decimal = decimal.slice(0, 2);
			
			  // 四舍五入处理（如果需要）
			  if (decimal.length === 2 && input !== `${integer}.${decimal}`) {
			    const rounded = Math.round(parseFloat(`${integer}.${decimal.padEnd(3, '0')}`) * 100) / 100;
			    [integer, decimal] = rounded.toFixed(2).split('.');
			  }
			
			  // 组合最终结果
			  let result = integer;
			  if (decimal) result += `.${decimal}`;
			
			  // 转回数字类型（保留原始数据类型特性）
			  return result.includes('.') ? parseFloat(result) : parseInt(result);
			},
			onInput(e) {
				var tmp = this.formatTwoDecimals(e.target.value)
			    this.money = tmp;
		    },
			doPay(){
				var that = this;
				uni.login({
					success: (e) => {
						console.log(e.code);
						// this.wanlPay(e.code);
						
						that.nextPay(e.code)
						
					},
					fail: (e) => {
						uni.showModal({
							content: "无法获取微信code,原因为: " + e.errMsg,
							showCancel: false
						})
					}
				})
			},
			async nextPay(code){
				await uni.request({
					url: '/wanlshop/pay/payServerCharge',
					method: 'POST',
					data: {
						code: code,
						price:this.money
					},
					success: res => {
						uni.requestPayment({
							appId: res.data.appId,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							paySign: res.data.paySign,
							signType: res.data.signType,
							timeStamp: res.data.timeStamp,
							success: (e) => {
								this.$st.success("支付成功", () => {
									uni.navigateBack();
								})
								// console.log(e, '支付成功');
								// uni.request({
								// 	url: '/wanlshop/user/joinVip',
								// 	method: 'POST',
								// 	data: {
								// 		vip_id: this.vip.vip_id,
								// 		vip_user_id: res.data.vip_user.vip_user_id
								// 	},
								// 	success: (e) => {
								// 		uni.showToast({
								// 			'complete': 1500,
								// 			'title': '开通成功',
								// 			'icon': 'success'
								// 		})
								// 		setTimeout(() => {
								// 			uni.navigateBack(); // 返回上一页
								// 		}, 1500);
								// 	},
								// });
							},
							fail: (e) => {
								uni.showModal({
									content: "支付失败",
									showCancel: false
								})
							}
						})
					}
				});
			}
		}
	}
</script>

<style>
	page {
		background-color: #ffffff;
	}
	.title {
		width: 606rpx;
		font-size: 30rpx;
		color: #3D3D3D;
		margin-left: 72rpx;
		margin-top: 95rpx;
	}

	.content {
		width: 606rpx;
		height: 132rpx;
		display: flex;
		margin-left: 72rpx;
		align-items: center;
		border-bottom: 1px solid #D8D8D8;
		margin-top: 20rpx;
	}

	.dole {
		font-size: 53rpx;
		color: #3D3D3D;
		font-weight: 700;
	}

	.money {
		font-size: 53rpx;
		color: #3D3D3D;
		font-weight: 700;
		height: 132rpx;
		line-height: 132rpx;
	}
	
	.buttonBox {
		position: fixed;
		bottom: 0;
		width: 750rpx;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;
		/* padding-bottom: env(safe-area-inset-bottom); */
		padding-bottom:100rpx;
	}
	
	.recharge {
		width: 416rpx;
		height: 82rpx;
		border-radius: 50rpx;
		background-color: #84BD00;
		color: #ffffff;
		text-align: center;
		line-height: 82rpx;
	}
</style>